package top.eggcode.modules.user.store;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.support.JpaRepositoryImplementation;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import top.eggcode.modules.user.model.AccountEntity;
import top.eggcode.modules.user.model.UserQuery;

import java.util.List;

/**
 * Title: 账户存储
 * Description: TODO
 * Date: 2021/3/27 11:39
 *
 * @author JiaQi Ding
 * @version 1.0
 */

@Repository
public interface AccountRepository extends JpaRepositoryImplementation<AccountEntity, String> {

    /**
     * 查询账户
     *
     * @param username 用户名
     * @param password 密码
     * @return 账户
     */
    List<AccountEntity> findByUsernameAndPassword(String username, String password);

    /**
     * 查找账户
     *
     * @param username 用户名
     * @return 账户
     */
    List<AccountEntity> findByUsername(String username);

    /**
     * 查找，根据用户编号
     *
     * @param userId 用户主键
     * @return 账户
     */
    AccountEntity findByUserId(String userId);


    @Query(value = "SELECT *" +
            " FROM system_account root" +
            " LEFT JOIN user_info join_user ON root.user_id = join_user.id" +
            " WHERE join_user.nickname LIKE concat(concat('%', :#{#query.nickname}), '%')" +
            " AND join_user.sex = :#{#query.sex}" +
            " AND root.username LIKE concat(concat('%', :#{#query.username}), '%')", nativeQuery = true)
    Page<AccountEntity> listPage(@Param("query") UserQuery query,
                                 Pageable pageable);
}
